package team.fourth.combat.web;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import team.fourth.combat.commen.entity.Result;
import team.fourth.combat.service.ReportService;

import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    ReportService reportService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        List<String> months = new ArrayList<>();

        //1、从当前时间往前推12个月
        Date date = new Date();
        DateTime dateTime = DateUtil.offsetMonth(date, -12);//2019-02

        for (int i = 0; i <= 12; i++) {
            //往后偏移12个月
            DateTime monthDate = DateUtil.offsetMonth(dateTime, i);
            months.add(monthDate.toString("yyyy-MM"));
        }


        List<Integer> memberCount =  reportService.getMemberReport(months);

        Map map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);

        return Result.success("",map);

    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
       List<Map> maps =  reportService.getSetmealReport();

//       [{value: 335, name: '直接访问'},
//        {value: 310, name: '邮件营销'},
//        {value: 234, name: '联盟广告'},
//        {value: 135, name: '视频广告'},
//        {value: 1548, name: '搜索引擎'}]
        return Result.success("",maps);
    }


    public static void main(String[] args) {
        //得到过去一年时间内每个月构建成一个数组 ['2019-10', '2019-11', '2019-12', '2020-01', '2020-02', '2020-03', '2020-03']


        List<String> months = new ArrayList<>();

        //1、从当前时间往前推12个月
        Date date = new Date();
        DateTime dateTime = DateUtil.offsetMonth(date, -12);//2019-02

        for (int i = 0; i <= 12; i++) {
            //往后偏移12个月
            DateTime monthDate = DateUtil.offsetMonth(dateTime, i);
            months.add(monthDate.toString("yyyy-MM"));
        }

        System.out.println(months);

    }
}
